package com.spring.campus_express_delivery.controller;

import com.auth0.jwt.JWT;
import com.spring.campus_express_delivery.entity.ExpressCompany;
import com.spring.campus_express_delivery.entity.PageBean;
import com.spring.campus_express_delivery.entity.Users;
import com.spring.campus_express_delivery.interceptor.RequiredPermission;
import com.spring.campus_express_delivery.service.ExpressCompanyService;
import com.spring.campus_express_delivery.service.UsersService;
import com.spring.campus_express_delivery.util.JwtToken;
import com.spring.campus_express_delivery.util.MyUtil;
import com.spring.campus_express_delivery.util.RedisUtil;
import com.spring.campus_express_delivery.util.ResponseEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@CrossOrigin
@Controller
@RequestMapping("/express_company")
@Api(value = "ExpressCompanyController", description = "取件地址相关api")
public class ExpressCompanyController {
    Boolean flag;
    MyUtil myUtil = new MyUtil();
    @Autowired
    ExpressCompanyService expressCompanyService;
    @Autowired
    UsersService usersService;
    @Autowired
    RedisUtil redisUtil;

    /**
     * 取件地址列表
     *
     * @param size
     * @param page
     * @param sort
     * @param asc
     * @param Authorization
     * @return
     * @throws IOException
     */
    @ApiOperation("取件地址列表")
    @GetMapping("/wx/expressCompany_list")
    @ResponseBody
    public ResponseEntity selectExpressCompanyByPage(@RequestParam(value = "size", required = false, defaultValue = "10") int size,
                                                     @RequestParam(value = "page", required = false, defaultValue = "1") int page,
                                                     @RequestParam(value = "sort", required = false) String sort,
                                                     @RequestParam(value = "asc", required = false) String asc,
                                                     @RequestHeader String Authorization) throws IOException {

        ResponseEntity responseEntity;
        String token = Authorization;
        PageBean<ExpressCompany> pagemsg = expressCompanyService.selectExpressCompanyByPage(size, page, sort, asc);
        responseEntity = ResponseEntity.ok();
        responseEntity.putDataValue("records", pagemsg);
        return responseEntity;
    }

    /**
     * 添加取件地址类型
     *
     * @param expressCompanyType
     * @param Authorization
     * @throws IOException
     * @rueturn
     */
    @ApiOperation("添加取件地址类型")
    @PostMapping("/web/insertExpressCompanyType")
    @ResponseBody
    @RequiredPermission(1)
    public ResponseEntity insertExpressCompanyType(@RequestParam(required = true) String expressCompanyType, @RequestHeader String Authorization) throws IOException {
        ResponseEntity responseEntity;
        String token = Authorization;
        Users users = JwtToken.unsign(token, Users.class);
        ExpressCompany expressCompany1 = expressCompanyService.selectByExpressCompanyType(expressCompanyType);
        if (expressCompany1 == null) {
            responseEntity = ResponseEntity.ok();
            ExpressCompany expressCompany = new ExpressCompany();
            expressCompany.setExpressCompanyType(expressCompanyType);
            expressCompanyService.insertExpressCompanyType(expressCompany);
            responseEntity.putDataValue("msg", "添加成功");
            return responseEntity;
        } else {
            responseEntity = ResponseEntity.forbidden();
            responseEntity.putDataValue("msg", "快递公司类型已存在无法添加");
            return responseEntity;
        }
    }

    /**
     * 修改快递取件地址类型
     *
     * @param id
     * @param expressCompanyType
     * @param Authorization
     * @return
     * @throws IOException
     */
    @ApiOperation("修改快递取件地址类型")
    @PostMapping("/web/updateExpressCompanyType")
    @ResponseBody
    @RequiredPermission(1)
    public ResponseEntity updateExpressCompanyType(@RequestParam(required = true) int id, @RequestParam(required = false) String expressCompanyType, @RequestHeader String Authorization) throws IOException {
        ResponseEntity responseEntity;
        String token = Authorization;
        Users users = JwtToken.unsign(token, Users.class);
        ExpressCompany expressCompany = expressCompanyService.selectByExpressId(id);

        if (expressCompany.getExpressCompanyType() != expressCompanyType) {
            responseEntity = ResponseEntity.ok();
            expressCompany.setExpressCompanyType(expressCompanyType);
            expressCompanyService.updateExpressCompanyType(expressCompany);
            responseEntity.putDataValue("msg", "修改成功");
            //删除缓存
            String key = "expressCompany_" + id;
            if (redisUtil.hasKey(key)) {
                redisUtil.del(key);
            }
            return responseEntity;
        } else {
            responseEntity = ResponseEntity.forbidden();
            responseEntity.putDataValue("msg", "快递公司类型已存在，修改成另一个快递公司类型");
            return responseEntity;
        }


    }

    /**
     * 根据id查询快递取件地址
     *
     * @param id
     * @param Authorization
     * @return
     * @throws IOException
     */
    @ApiOperation("根据id查询快递取件地址类型")
    @GetMapping("/web/selectById")
    @ResponseBody
    public ResponseEntity selectById(@RequestParam(required = true) Integer id, @RequestHeader String Authorization) throws IOException {
        ResponseEntity responseEntity;
        String token = Authorization;
        Users users = JwtToken.unsign(token, Users.class);
        //查询缓存
        String key = "expressCompany_" + id;
        boolean b = redisUtil.hasKey(key);
        //如果redis中存在 查询缓存
        if (b){
            ExpressCompany expressCompany = (ExpressCompany) redisUtil.get(key);
            responseEntity=ResponseEntity.ok();
            responseEntity.putDataValue("expressCompany",expressCompany);
            return responseEntity;
        }
        //redis中没有，从数据库中查询
        responseEntity=ResponseEntity.ok();
        ExpressCompany expressCompany = expressCompanyService.selectByExpressId(id);
        responseEntity.putDataValue("expressCompany",expressCompany);
        //将查询到的数据缓存到redis中
        redisUtil.set("expressCompany_" + id, expressCompany);
        return responseEntity;
    }

    /**
     * 根据id删除快递取件地址
     * @param id
     * @param Authorization
     * @return
     * @throws IOException
     */
    @ApiOperation("根据id删除快递取件地址类型")
    @DeleteMapping("/web/deleteExpressCompanyById")
    @ResponseBody
    @RequiredPermission(1)
    public ResponseEntity deleteExpressCompanyById(@RequestParam(required = true) Integer id,@RequestHeader String Authorization)throws IOException{
        String token=Authorization;
        Users users=JwtToken.unsign(token,Users.class);
        ExpressCompany expressCompany = expressCompanyService.selectByExpressId(id);
        ResponseEntity responseEntity;
        if(expressCompany!=null){
                expressCompanyService.deleteExpressCompanyById(id);
                responseEntity=ResponseEntity.ok();
                responseEntity.putDataValue("msg","删除成功");
                //删除缓存
                String key = "expressCompany_" + id;
                if (redisUtil.hasKey(key)){
                    redisUtil.del(key);
                }
                return responseEntity;
        }else{
            responseEntity=ResponseEntity.forbidden();
            responseEntity.putDataValue("msg","快递取件地址不存在，删除失败");
            return responseEntity;
        }
    }
}
